﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace TaDMS.Models
{
    public class JobTitleRepository : Repository<JobTitle>
    {
        public JobTitleRepository(DbContext model) : base(model)
        {
        }

        public override IEnumerable<JobTitle> GetData(out int? total, Func<IQueryable<JobTitle>, IOrderedQueryable<JobTitle>> orderBy, Expression<Func<JobTitle, bool>> filter = null, IList<Expression<Func<JobTitle, bool>>> multiFilter = null,  string includeProperties = "", bool isPaging = false, int start = 0, int limit = 50)
        {
            try
            {
                IQueryable<JobTitle> query = base.DbSet;
                IEnumerable<JobTitle> result = null;

                if (filter != null)
                {
                    query = query.Where(filter);
                }
                if (multiFilter != null && multiFilter.Any())
                {
                    query = multiFilter.Aggregate(query, (current, expression) => current.Where(expression));
                }

                query = includeProperties.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Aggregate(query, (current, includeProperty) => current.Include(includeProperty));

                if (isPaging)
                {
                    total = query.Count();

                    query = orderBy != null ? orderBy(query) : query;

                    query = query.Skip(start).Take(limit);

                    result = query.ToList().Select(t => new JobTitle { JobTitleId = t.JobTitleId, Desicriptions = t.Desicriptions, JobTitleName = t.JobTitleName, ParentJobTitleId = t.ParentJobTitleId });

                    //total = result.Count();
                }
                else
                {
                    total = null;
                }

                return result;

            }
            catch (Exception exception)
            {
                this.ErrorMessage = exception.Message;

                total = null;

                return null;
            }
        }

        public List<JobTitle> GetJobTitleTree()
        {
            using (var db = new TaDMS())
            {
                var parentJobTitle = db.sp_JobTitle_GetRoot().ToList();

                var result = parentJobTitle.Select(rootResult => new JobTitle() { JobTitleId = rootResult.JobTitleId, JobTitleName = rootResult.JobTitleName, Desicriptions = rootResult.Desicriptions, ParentJobTitleId = rootResult.ParentJobTitleId }).ToList();

                GetList(result);

                return result;
            }
        }

        /*Private method*/
        private void GetList(IEnumerable<JobTitle> source)
        {
            foreach (var jobTitle in source)
            {
                var newSource = new List<JobTitle>();
                using (var db = new TaDMS())
                {
                    var data = db.sp_JobTitle_GetChildren(jobTitle.JobTitleId).AsEnumerable();
                    newSource.AddRange(data.Select(result => new JobTitle() { JobTitleId = result.JobTitleId, JobTitleName = result.JobTitleName, Desicriptions = result.Desicriptions, ParentJobTitleId = result.ParentJobTitleId }));
                }

                jobTitle.JobTitle1 = newSource;

                this.GetList(newSource);
            }
        }
    }
}